package com.cheer.ba.widget.headview;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.LinearLayout;
import android.widget.Scroller;
import android.widget.TextView;

import com.andview.refreshview.callback.IHeaderCallBack;
import com.cheer.ba.R;
import com.cheer.ba.utils.StringUtils;
import com.cheer.ba.widget.MyGifView;

/**
 * 自定义车跑的动画
 * Created by Hq on 2017/7/13.
 */
public class CheerHeadView extends LinearLayout implements IHeaderCallBack {

    private Context mContext;

    public CheerHeadView(Context context) {
        this(context, null);
    }

    public CheerHeadView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public CheerHeadView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public CheerHeadView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }

    private MyGifView mLoadImg;
    private TextView mTxtTitle;
    private TextView mTxtTime;
    //是否刷新完成
    private boolean finished = false;
    //是否正在刷新
    private boolean refreshing = false;
    //头部是否移动
    private boolean hasHeaderMove = false;
    //记录上一次刷新的时间
    private long mRecordLastTime;
    //记录显示的文字
    private String mRecordTitle;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            mTxtTitle.setText(mRecordTitle);
            mTxtTime.setText("上一次刷新：" + StringUtils.friendlyTime(mRecordLastTime));
        }
    };

    /**
     * 初始化
     */
    private void init(Context context) {
        this.mContext = context;
        View contentView = LayoutInflater.from(context).inflate(R.layout.cheer_car_headview, this);
        mLoadImg = (MyGifView) contentView.findViewById(R.id.id_load_gif_view);
        mTxtTitle = (TextView) contentView.findViewById(R.id.id_refresh_title_txt);
        mTxtTime = (TextView) contentView.findViewById(R.id.id_refresh_time_txt);
        mLoadImg.setMovieResource(R.raw.ic_loading);

        mRecordTitle = "下拉可以刷新哦";
        mRecordLastTime = System.currentTimeMillis();
        handler.sendEmptyMessage(0);
    }

    /**
     * 正常状态
     */
    @Override
    public void onStateNormal() {
        finished = false;
        refreshing = false;

        mRecordTitle = "下拉可以刷新哦";
        handler.sendEmptyMessage(0);
    }

    /**
     * 准备刷新
     */
    @Override
    public void onStateReady() {
        finished = false;
        refreshing = false;

        mRecordTitle = "快放开我吧，我要刷新了";
        handler.sendEmptyMessage(0);
    }

    /**
     * 正在刷新
     */
    @Override
    public void onStateRefreshing() {
        finished = false;
        refreshing = true;

        mRecordTitle = "正在刷新...";
        handler.sendEmptyMessage(0);
    }

    /**
     * 刷新结束
     *
     * @param success 是否刷新成功 success参数由XRefreshView.stopRefresh(boolean)传入
     */
    @Override
    public void onStateFinish(boolean success) {
        finished = true;
        refreshing = false;
        hasHeaderMove = false;

        mRecordTitle = "刷新完成";
        handler.sendEmptyMessage(0);
    }

    /**
     * 获取headerview显示的高度与headerview高度的比例
     *
     * @param headerMovePercent 移动距离和headerview高度的比例
     * @param offsetY           headerview移动的距离
     */
    @Override
    public void onHeaderMove(double headerMovePercent, int offsetY, int deltaY) {
        if (finished || refreshing) return;
        hasHeaderMove = true;
    }

    /**
     * 设置显示上一次刷新的时间
     *
     * @param lastRefreshTime 上一次刷新的时间
     */
    @Override
    public void setRefreshTime(long lastRefreshTime) {
        mRecordLastTime = lastRefreshTime;
    }

    /**
     * 隐藏footerview
     */
    @Override
    public void hide() {
        setVisibility(View.GONE);
    }

    /**
     * 显示footerview
     */
    @Override
    public void show() {
        setVisibility(View.VISIBLE);
    }

    /**
     * 获得headerview的高度,如果不想headerview全部被隐藏，就可以只返回一部分的高度
     *
     * @return
     */
    @Override
    public int getHeaderHeight() {
        return getMeasuredHeight();
    }
}
